import axios from "axios";
// 引入进度条和 样式
import nprogress from "nprogress";
import "nprogress/nprogress.css";

// 引入vuex，以获取身份信息
// import store from "@/store";

const requests = axios.create({
	baseURL: "/api",
	timeout: 5000,
});

// 监测到请求后
requests.interceptors.request.use(
	(config) => {
		// config请求的配置对象
		nprogress.start();

		// userTempId为服务器规定字段
		if (localStorage.getItem("USER_UUID")) {
			config.headers.userTempId = localStorage.getItem("USER_UUID");
		}

		// 请求头携带token
		if (localStorage.getItem("TOKEN")) {
			config.headers.token = localStorage.getItem("TOKEN");
		}

		return config;
	}
	// (err) => {}
);

// 监测到请求后
requests.interceptors.response.use(
	(res) => {
		nprogress.done();
		return res.data;
	}
	// (err) => {}
);

export default requests;
